package cmd

import (
	"fmt"
	"os"
	"strings"

	"github.com/greenled/portainer-stack-utils/version"

	"github.com/sirupsen/logrus"

	"github.com/mitchellh/go-homedir"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var settingsFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:     "psu",
	Short:   "A CLI client for Portainer",
	Version: "is set on common/version.CurrentVersion",
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func init() {
	cobra.OnInitialize(initSettings, initLogger)

	rootCmd.SetVersionTemplate("{{ version }}\n")
	cobra.AddTemplateFunc("version", version.BuildVersionString)

	rootCmd.PersistentFlags().StringVar(&settingsFile, "settings-file", "", "Settings file. (default \"$HOME/.psu.yaml)\"")
	rootCmd.PersistentFlags().StringP("log-level", "v", "info", "Log level. One of trace, debug, info, warning, error, fatal or panic.")
	rootCmd.PersistentFlags().String("log-format", "text", "Log format. One of text or json.")
	rootCmd.PersistentFlags().BoolP("insecure", "i", false, "Skip Portainer SSL certificate verification.")
	rootCmd.PersistentFlags().StringP("url", "l", "", "Portainer url.")
	rootCmd.PersistentFlags().StringP("user", "u", "", "Portainer user.")
	rootCmd.PersistentFlags().StringP("password", "p", "", "Portainer password.")
	rootCmd.PersistentFlags().StringP("auth-token", "A", "", "Portainer auth token.")
	rootCmd.PersistentFlags().DurationP("timeout", "t", 0, "Waiting time before aborting (like 100ms, 30s, 1h20m).")
	viper.BindPFlag("settings-file", rootCmd.PersistentFlags().Lookup("settings-file"))
	viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level"))
	viper.BindPFlag("log-format", rootCmd.PersistentFlags().Lookup("log-format"))
	viper.BindPFlag("insecure", rootCmd.PersistentFlags().Lookup("insecure"))
	viper.BindPFlag("url", rootCmd.PersistentFlags().Lookup("url"))
	viper.BindPFlag("timeout", rootCmd.PersistentFlags().Lookup("timeout"))
	viper.BindPFlag("user", rootCmd.PersistentFlags().Lookup("user"))
	viper.BindPFlag("password", rootCmd.PersistentFlags().Lookup("password"))
	viper.BindPFlag("auth-token", rootCmd.PersistentFlags().Lookup("auth-token"))
}

// initSettings reads in setting file and ENV variables if set.
func initSettings() {
	if settingsFile != "" {
		// Use setting file from the flag.
		viper.SetConfigFile(settingsFile)
	} else {
		// Find home directory.
		home, err := homedir.Dir()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		// Search settings file in home directory with name ".psu" (without extension).
		viper.AddConfigPath(home)
		viper.SetConfigName(".psu")
	}

	viper.SetEnvPrefix("PSU")
	viper.AutomaticEnv() // read in environment variables that match

	replacer := strings.NewReplacer("-", "_", ".", "_")
	viper.SetEnvKeyReplacer(replacer)

	// If a setting file is found, read it in.
	viper.ReadInConfig()
}

func initLogger() {
	logLevelName := viper.GetString("log-level")
	logLevel, err := logrus.ParseLevel(logLevelName)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"level":        logLevelName,
			"implications": `Default "info" level will be used instead`,
		}).Warning("Unknown log level")
		logrus.SetLevel(logrus.InfoLevel)
	}
	logrus.SetLevel(logLevel)

	switch viper.GetString("log-format") {
	case "json":
		logrus.SetFormatter(&logrus.JSONFormatter{})
	case "text":
		logrus.SetFormatter(&logrus.TextFormatter{})
	default:
		logrus.WithFields(logrus.Fields{
			"format":       viper.GetString("log-format"),
			"implications": `Default text format will be used instead`,
		}).Warning("Unknown log format")
		logrus.SetFormatter(&logrus.TextFormatter{})
	}
}
